package com.tgy.leetcode;

/**
 *
 * 给你两个单链表的头节点 headA 和 headB ，请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点，返回 null 。
 */
public class _160_相交链表_01 {

    public static ListNode getIntersectionNode(ListNode headA, ListNode headB) {

        int aLen = 0;
        int bLen = 0;

        ListNode tmpNodeA = headA;
        ListNode tmpNodeB = headB;
        while (tmpNodeA != null) {

            aLen++;
            tmpNodeA = tmpNodeA.next;
        }

        while (tmpNodeB != null) {

            bLen++;
            tmpNodeB = tmpNodeB.next;
        }

        tmpNodeA = headA;
        tmpNodeB = headB;
        if (aLen > bLen) {

            while (aLen > bLen) {

                tmpNodeA = tmpNodeA.next;
                aLen--;
            }
        }else if(aLen < bLen) {

            while (aLen < bLen) {

                tmpNodeB = tmpNodeB.next;
                bLen--;
            }
        }

        while (tmpNodeA != null && tmpNodeB != null) {

            if (tmpNodeA == tmpNodeB) {

                return tmpNodeA;
            }

            tmpNodeA = tmpNodeA.next;
            tmpNodeB = tmpNodeB.next;
        }

        return null;
    }
    public static void main(String[] args) {


    }
}
